package test.sgone.cn.mulimagedemo;

import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;

public class BrowerImageActivity extends AppCompatActivity {
    private ArrayList<View> listViews = null;
    private ViewPager pager;
    private MyPagerAdapter adapter;
    private Button backBtn;
    private Button delBtn;
    private ArrayList<String> selectedData = new ArrayList<>();
    private int curPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brower_image);
        initView();
    }
    private void initView()
    {
        selectedData = (ArrayList)getIntent().getExtras().getSerializable("dataList");
        curPosition = (int)getIntent().getExtras().getInt("position");
        for(int i=0;i<selectedData.size();i++)
        {
            initListViews(ImageUtils.getBitmapByPath(selectedData.get(i)));
        }
        backBtn = (Button)findViewById(R.id.btn_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        delBtn = (Button)findViewById(R.id.btn_del);
        delBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                    finish();
            }
        });

        pager = (ViewPager)findViewById(R.id.viewpager);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                curPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        adapter = new MyPagerAdapter(listViews);
        pager.setAdapter(adapter);
        pager.setCurrentItem(curPosition);

    }

    private void initListViews(Bitmap bm) {
        if (listViews == null)
            listViews = new ArrayList<View>();
        ImageView img = new ImageView(this);// 构造textView对象
        img.setBackgroundColor(0xff000000);
        img.setImageBitmap(bm);
        img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        listViews.add(img);// 添加view
    }


}
